<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'pig_estate_add'">
            新 增
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            v-auth="'pig_estate_del'" @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'pig_estate_export'"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
          <el-table-column label="小区ID" align="center" prop="id" width="90"/>
          <el-table-column label="小区名称" align="center" prop="estateName" width="150" />
          <el-table-column label="所属物业" align="center" prop="propertyName" width="110" />
          <el-table-column label="负责人" align="center" prop="leaderName"  />
          <el-table-column label="负责人电话" align="center" prop="leaderPhone"  width="130" />
          <el-table-column label="办公室电话" align="center" prop="telphone" width="130"/>
          <el-table-column label="二维码下载" align="center" width="150">
              <template #default="scope">
                  <el-popover placement="right" width="400" trigger="hover">
                      <el-image style="width: 380px; height: 380px" :src="scope.row.qrCode"  fit="fill" />
                      <i class="el-icon-picture" slot="reference"/>
                  </el-popover>
                  <el-button size="mini" type="text" icon="el-icon-download"
                             @click="downloadImg(scope.row)">{{scope.row.estateName }}</el-button>
              </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="state">
              <template #default="scope">
                  <div>
                      <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="''">正常</el-tag>
                      <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="'info'">停用</el-tag>
                  </div>
              </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="420" fixed="right">
              <template #default="scope">
                  <el-dropdown size="mini" @command="(command) => goUserList(command, scope.row)" style="margin-right: 10px;">
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>用户管理 &nbsp;
                </span>
                      <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item command="bandWxUser" icon="el-icon-key">项目经理/客服</el-dropdown-item>
                          <el-dropdown-item command="worker" icon="el-icon-circle-check">维修师父</el-dropdown-item>
                          <el-dropdown-item command="checkUser" icon="el-icon-circle-check">抄表人员</el-dropdown-item>
                      </el-dropdown-menu>
                  </el-dropdown>

                  <el-button size="mini" type="text" icon="el-icon-user-solid" >
                      <router-link :to="'/property/estate/rooms?estateId=' + scope.row.id" class="link-type">
                          住宅列表
                      </router-link>
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-user-solid">
                      <router-link :to="'/property/estate/rooms?estateId=' + scope.row.id+'&qianfei=1'" class="link-type">
                          物业费缴费记录
                      </router-link>
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-user-solid">
                      <router-link :to="'/property/estate/rooms?estateId=' + scope.row.id+'&qianfei=0'" class="link-type">
                          物业费欠费明细
                      </router-link>
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-picture" @click="handleUpdateSinglePrice(scope.row)"
                             v-auth="'module:estate:edit'">水电费单价配置</el-button>
                  <el-button size="mini" type="text" icon="el-icon-picture" @click="resetQrCode(scope.row)"
                             v-auth="'module:estate:edit'">生成二维码</el-button>
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                             v-auth="'module:estate:edit'"> 修改</el-button>
              </template>
          </el-table-column>
<!--          <el-table-column label="操作" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-button icon="edit-pen" text type="primary" v-auth="'pig_estate_edit'"-->
<!--                @click="formDialogRef.openDialog(scope.row.id)">编辑</el-button>-->
<!--              <el-button icon="delete" text type="primary" v-auth="'pig_estate_del'" @click="handleDelete([scope.row.id])">删除</el-button>-->
<!--            </template>-->
<!--          </el-table-column>-->
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemEstate">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/platform/module/estate/estate";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/pig/estate/export',Object.assign(state.queryForm, { ids: selectObjs }), 'estate.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
